/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WekaModels;

import grex.Data.ArffTableModel;
import grex.Environment;
import grex.ErrorManager;
import grex.Prediction;
import grex.PredictionContainer;
import weka.classifiers.functions.LinearRegression;
import weka.core.Instance;
import weka.core.Instances;
import wekaGrexBridge.WekaArffTableModel;

/**
 *
 * @author RIK
 */
public class GrexLinearRegression extends WekaPredictiveModel{
    private LinearRegression mreg;
    public GrexLinearRegression(ArffTableModel data){
        super(data,new LinearRegression());
        mreg = (LinearRegression) model;        
    }
    public GrexLinearRegression(Environment env){
        super(env,new LinearRegression());
        mreg = (LinearRegression) model;  
    }
      

    public double getNrOfNodes() {
        return mreg.numParameters();
    }
    
    @Override
        public void execute(PredictionContainer pc) {
        for (Prediction p : pc.values()) {

            try {
                Instance instance = wekaArffTableModel.getInstance(p.getInstance(),wekaTrain);//wekaTrain is just used to set the Dataset in the instance
                double prediction;
                prediction = Math.max(model.classifyInstance(instance),0);
                
                p.setProbs(model.distributionForInstance(instance));
                p.setPrediction(prediction);
             
            } catch (Exception ex) {
                ErrorManager.getInstance().reportError(ex);
            }

        }
    }
    
    public String toString(){
        String s = "";
        for(int i=0; i < mreg.coefficients().length;i++){
            s+="c"+i+":"+mreg.coefficients()[i]+" ";
        }
        return s;
    }

    public String getName() {
        return "MREG";
    }
    
}
